package com.zycfc.zsf.boot.mybatis.generator.codegen.db.impl;

import com.zycfc.zsf.boot.mybatis.generator.codegen.db.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.model.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.util.*;
import java.sql.*;

public class MySqlMapCmd implements MapCmd<ColumnModel>
{
    @Override
    public ColumnModel getObjecFromRs(final ResultSet rs) throws SQLException {
        final ColumnModel model = new ColumnModel();
        final String colName = rs.getString("column_name");
        final String humpColName = StringUtil.underLineToHump(colName.toLowerCase());
        final String column_key = rs.getString("column_key");
        final long character_length = rs.getLong("character_octet_length");
        final String nullable = rs.getString("is_nullable");
        final boolean is_nullable = nullable.equals("YES");
        final int precision = rs.getInt("numeric_precision");
        final int scale = rs.getInt("NUMERIC_scale");
        String comment = rs.getString("column_comment");
        comment = (StringUtil.isEmpty(comment) ? colName : comment);
        final boolean isPk = column_key.equals("PRI");
        final String data_type = rs.getString("data_type");
        final String javaType = this.getJavaType(data_type, precision, scale);
        final String displayDbType = this.getDisplayDbType(data_type, character_length, precision, scale);
        final String[] aryComment = comment.split("\n");
        model.setColumnName(colName);
        model.setHumpColumnName(humpColName);
        model.setIsNotNull(!is_nullable);
        model.setPrecision(precision);
        model.setScale(scale);
        model.setLength(character_length);
        model.setComment(aryComment[0]);
        model.setIsPK(isPk);
        model.setColDbType(data_type);
        model.setColType(javaType);
        model.setDisplayDbType(displayDbType);
        return model;
    }
    
    private String getDisplayDbType(final String dbtype, final long character_length, final int precision, final int scale) {
        if (dbtype.equals("varchar")) {
            return "varchar(" + character_length + ")";
        }
        if (dbtype.equals("decimal")) {
            return "decimal(" + precision + "," + scale + ")";
        }
        return dbtype;
    }
    
    private String getJavaType(final String dbtype, final int precision, final int scale) {
        if (dbtype.equals("bigint")) {
            return "Long";
        }
        if (dbtype.equals("int")) {
            return "Integer";
        }
        if (dbtype.equals("tinyint") || dbtype.equals("smallint")) {
            return "Short";
        }
        if (dbtype.equals("varchar") || dbtype.endsWith("text") || dbtype.equals("char")) {
            return "String";
        }
        if (dbtype.equals("double")) {
            return "Double";
        }
        if (dbtype.equals("float")) {
            return "Float";
        }
        if (dbtype.endsWith("blob")) {
            return "byte[]";
        }
        if (dbtype.equals("decimal")) {
            if (scale != 0) {
                return "BigDecimal";
            }
            if (precision <= 10) {
                return "Integer";
            }
            return "Long";
        }
        else {
            if (dbtype.startsWith("date")) {
                return "java.util.Date";
            }
            return dbtype;
        }
    }
}
